<?php

/**
 * This is the model class for table "{{banner_details}}".
 *
 * The followings are the available columns in table '{{banner_details}}':
 * @property integer $id
 * @property string $title
 * @property string $slug
 * @property string $image
 * @property string $page
 * @property string $location_tags
 * @property string $created
 * @property string $modified
 */
class BannerDetails extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return BannerDetails the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_banner_details';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, page, location_tags, height, width', 'required'),
			array('height, width', 'numerical', 'integerOnly'=>true),
			array('image', 'required','on'=>'insert'),
			array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true ,'on'=>'update'),
			array('title, slug', 'length', 'max'=>45),
			array('image', 'length', 'max'=>400),
			array('page, location_tags', 'length', 'max'=>100),
			array('created, modified', 'safe'),
			array('modified','default',
           	  'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'update'),
        	array('created,modified','default',
              'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),
			array('id, title', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'slug' => 'Slug',
			'image' => 'Image',
			'page' => 'Page',
			'location_tags' => 'Location Tags',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
			$page = null;
		if (isset($this->page) && $this->page != null)
		{
			$criteria=new CDbCriteria;
			$criteria->select = 'id' ;
			$criteria->condition = 'name LIKE :name' ;
			$criteria->params = array(':name'=>'%'.$this->page.'%') ;
			$page = Pages::model()->find($criteria);
		}
		
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		if (!empty($page))
		$criteria->compare('page',$page->id,true);



		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getModel($type,$arrParams)
	{
		switch ($type)
		{
			
			case 'BY-SLUG':
			return BannerDetails::model()->findByAttributes(array('slug'=>$arrParams['slug']));
			break;
		}
	}
}